﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SetRegionUC
{
    public partial class RegionUC : UserControl
    {
        #region Attributs and Getter/Setter
        private FVIL.Forms.CFviImageView m_imageView;
        public EventHandler eventCancelClick;
        public EventHandler eventApplyClick;
        private RectRegion m_rectRegion;

        public RectRegion RectRegion
        {
            get { return m_rectRegion; }
            set { m_rectRegion = value; }
        }

        public FVIL.Forms.CFviImageView ImageView
        {
            get { return m_imageView; }
            set { m_imageView = value; }
        }
        #endregion

        public RegionUC()
        {
            InitializeComponent();
        }


        #region Logic functions
        /// <summary>
        /// Initialize parameters
        /// </summary>
        /// <param name="imageView"></param>
        public void setImageView(FVIL.Forms.CFviImageView imageView)
        {
            m_imageView = imageView;
            m_imageView.Display.OverlayPatternRegist.Active = true;
            m_imageView.Refresh();
            m_imageView.Paint += new PaintEventHandler(OnPaintImageView);
        }

        /// <summary>
        /// Draw on image view
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnPaintImageView(object sender, PaintEventArgs e)
        {
            m_imageView.Display.OverlayPatternRegist.CenterMark.X =
                (double)m_imageView.Display.OverlayPatternRegist.RegistRect.Left;
            m_imageView.Display.OverlayPatternRegist.CenterMark.Y =
                (double)m_imageView.Display.OverlayPatternRegist.RegistRect.Top;
            m_rectRegion.TopX = m_imageView.Display.OverlayPatternRegist.RegistRect.Left.ToString();
            m_rectRegion.TopY = m_imageView.Display.OverlayPatternRegist.RegistRect.Top.ToString();
            m_rectRegion.BottomX = 
                m_imageView.Display.OverlayPatternRegist.RegistRect.Right.ToString();
            m_rectRegion.BottomY = 
                m_imageView.Display.OverlayPatternRegist.RegistRect.Bottom.ToString();
            pgSetRegion.SelectedObject = m_rectRegion;
        }

        /// <summary>
        /// Update overlay region from values in grid property
        /// </summary>
        private void updateOverlayRegist()
        {
            m_imageView.Display.OverlayPatternRegist.RegistRect.Left =
                Convert.ToDouble(m_rectRegion.TopX);
            m_imageView.Display.OverlayPatternRegist.RegistRect.Top =
                Convert.ToDouble(m_rectRegion.TopY);
            m_imageView.Display.OverlayPatternRegist.RegistRect.Right =
                Convert.ToDouble(m_rectRegion.BottomX);
            m_imageView.Display.OverlayPatternRegist.RegistRect.Bottom =
                Convert.ToDouble(m_rectRegion.BottomY);
            m_imageView.Display.OverlayPatternRegist.CenterMark.Y =
                m_imageView.Display.OverlayPatternRegist.RegistRect.Top;
            m_imageView.Display.OverlayPatternRegist.CenterMark.X =
                m_imageView.Display.OverlayPatternRegist.RegistRect.Left;
            m_imageView.Refresh();
        }

        private void hideRegistRegion()
        {
            m_imageView.Display.OverlayPatternRegist.Active = false;
            m_imageView.Paint -= new PaintEventHandler(OnPaintImageView);
            m_imageView.Refresh();
        }
        #endregion

        #region Events on control
        private void RegionUC_Load(object sender, EventArgs e)
        {
            m_rectRegion = new SetRegionUC.RectRegion("0", "0", "100", "100");
            pgSetRegion.SelectedObject = m_rectRegion;
        }

        public virtual void OnCancelClick()
        {
            EventHandler handler = eventCancelClick;
            if (handler != null)
            {
                handler(null, EventArgs.Empty);
            }
            hideRegistRegion();
        }

        public virtual void OnApplyClick()
        {
            EventHandler handler = eventApplyClick;
            if (handler != null)
            {
                handler(null, EventArgs.Empty);
            }
            hideRegistRegion();
        }


        private void pgSetRegion_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            if (!Number.isNumberBoundedBelow(e.ChangedItem.Value.ToString(), 0)
                || (Convert.ToInt32(e.ChangedItem.Value) > m_imageView.Image.HorzSize
                & (e.ChangedItem.Label == "TopX" || e.ChangedItem.Label == "BottomX"))
                || (Convert.ToInt32(e.ChangedItem.Value) > m_imageView.Image.VertSize
                & (e.ChangedItem.Label == "TopY" || e.ChangedItem.Label == "BottomY")))
            {
                e.ChangedItem.PropertyDescriptor.SetValue(pgSetRegion.SelectedObject, e.OldValue);
                return;
            }
            updateOverlayRegist();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            OnApplyClick();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            OnCancelClick();
        }
        #endregion

    }
}
